// Localization module

/// <reference path="../typings/jquery/jquery.d.ts" />
/// <reference path="../typings/jquery/jqueryui.d.ts" />


// delegate for event, when loading of messages is completed
export interface IMessagesLoaded {
    (): void;
}

// the class itself
export class Localization {

    // singleton instance
    private static mInstance: Localization = null;

    // messages container
    private mMessages: Array<Array<string>>;

    // constructor
    public constructor() {
        this.mMessages = null;
    }

    // singleton instance property getter
    public static get Instance() {
        if (Localization.mInstance == null)
            Localization.mInstance = new Localization();
        return Localization.mInstance;
    }

    // loads the messages
    public Load(completedHandler: IMessagesLoaded) {
        var that = this;
        // get JSON
        $.getJSON("/Scripts/messages.js", function (data, textStatus, jqXHR) {
            that.mMessages = data;
            completedHandler();                      
        });
    }

    // returns the string
    public Get(key: string): string {
        if (this.mMessages != null) {
            // iterate all messages
            for (var i = 0; i < this.mMessages.length; i++) {
                var msgPair = this.mMessages[i];
                if (msgPair[0] == key)
                    return msgPair[1];
            }
        }

        return "(Unknown)";
    }
}
